package worktools.utils;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by bearworker on 17-3-29.
 */
public class DbUtils {
    private Connection connection = null;

    private Properties properties = new Properties();

    ArrayList<Connection> connList = new ArrayList<>();

    public DbUtils(String filepath){
        FileInputStream fileInputStream = null;
        try {
            filepath = "".equals(filepath) || null == filepath ? "dbfile.properties" : filepath;
            File file = new File(filepath);
            fileInputStream = new FileInputStream(file);
            properties.load(fileInputStream);
        }catch (FileNotFoundException e){
            System.out.println("文件找不到");
        }catch (IOException e){
            System.out.println("文件读取错误");
        }finally {
            try {
                fileInputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }

        }
    }

    public DbUtils(boolean isPool,String filepath){
        this(filepath);
        if(isPool){
            this.createConnPool();
        }
    }

    public synchronized Connection getConn(){
        try {
            if(connection == null) {
                Class.forName(properties.getProperty("driver", "com.mysql.cj.jdbc.Driver"));
                connection = DriverManager.getConnection(properties.getProperty("url"), properties);
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return connection;
    }


    public ArrayList<Connection> createConnPool(){
        int maxSize = Integer.parseInt(properties.getProperty("maxSize"));
        try {
            Class.forName(properties.getProperty("driver", "com.mysql.cj.jdbc.Driver"));
            for(int i=0;i<maxSize;i++){
                Connection connection = DriverManager.getConnection(properties.getProperty("url"), properties);
                connList.add(connection);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return connList;
    }

    public Connection getConnByPool(){
        System.out.println(connList.size());

        for(Connection conn : connList){
            try {
                if(conn.isValid(0)){
                    if(connList.size()<=Integer.parseInt(properties.getProperty("minSize"))){
                        connection = this.getConn();
                        connList.add(connection);
                    }else {
                        connection = conn;
                        connList.remove(conn);
                    }
                    System.out.println(connection.getCatalog());
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return connection;
    }

    public void closeConn(Connection conn){
        try {
            if(conn.isValid(0)) {
                connList.add(conn);
            }else {
                connList.add(this.getConn());
            }
        }catch (Exception e){

        }
    }
}
